﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using HF.Framework.DataClientDBAgent;
using HF.WorkFlow.DataContract;
using HF.Framework.SqlDataContract;
using HF.WorkFlow.Const;
namespace HF.WorkFlow.Template
{
    /// <summary>
    /// 数据访问类WorkTaskVar。
    /// </summary>
    public class WorkTaskVar
    {
        #region  私有字段
        private SqlDataItem sqlDataItem = new SqlDataItem();
        private string[] fieldList = { "WorkTaskVarId", "WorkFlowId", "WorkTaskId", "VarName", "VarType", "TableName", "TableField", "InitValue", "AccessType" };
        private string tableName = "HF_WorkTaskVar";
        private string keyField = "WorkTaskVarId";
        private string sqlString = "";

        #endregion

        #region  私有方法
        private void setParameter(WorkTaskVarEntity entity)
        {
            sqlDataItem.ParameterList.Clear();
            sqlDataItem.AppendParameter("@WorkTaskVarId", entity.WorkTaskVarId, typeof(string));
            sqlDataItem.AppendParameter("@WorkFlowId", entity.WorkFlowId, typeof(string));
            sqlDataItem.AppendParameter("@WorkTaskId", entity.WorkTaskId, typeof(string));
            sqlDataItem.AppendParameter("@VarName", entity.VarName, typeof(string));
            sqlDataItem.AppendParameter("@VarType", entity.VarType, typeof(string));
            sqlDataItem.AppendParameter("@TableName", entity.TableName, typeof(string));
            sqlDataItem.AppendParameter("@TableField", entity.TableField, typeof(string));
            sqlDataItem.AppendParameter("@InitValue", entity.InitValue, typeof(string));
            sqlDataItem.AppendParameter("@AccessType", entity.AccessType, typeof(string));
        }

        private void setInsertSql()
        {
            string tmpValueList = "";
            string tmpFieldName = "";
            sqlString = "insert into " + tableName + "(";
            int tmpInt = this.fieldList.Length;
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + ", ";
                tmpValueList = tmpValueList + "@" + tmpFieldName + ",";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName;
            tmpValueList = tmpValueList + "@" + tmpFieldName;
            this.sqlString = sqlString + ")values(" + tmpValueList + ")";
            sqlDataItem.CommandText = sqlString;
        }

        private void setUpdateSql()
        {
            string tmpFieldName = "";
            int tmpInt = this.fieldList.Length;
            sqlString = "update " + tableName + " set ";
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName + ", ";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName;
            sqlString = sqlString + " where " + keyField + "=@" + this.keyField;
            sqlDataItem.CommandText = sqlString;
        }

        public WorkTaskVarEntity GetWorkTaskVarEntity(DataRow dr)
        {
            WorkTaskVarEntity entity = new WorkTaskVarEntity();
            entity.WorkTaskVarId = dr["WorkTaskVarId"].ToString();
            entity.WorkFlowId = dr["WorkFlowId"].ToString();
            entity.WorkTaskId = dr["WorkTaskId"].ToString();
            entity.VarName = dr["VarName"].ToString();
            entity.VarType = dr["VarType"].ToString();
            entity.TableName = dr["TableName"].ToString();
            entity.TableField = dr["TableField"].ToString();
            entity.InitValue = dr["InitValue"].ToString();
            entity.AccessType = dr["AccessType"].ToString();
            return entity;
        }

        #endregion

        #region  构造函数
        public WorkTaskVar()
        { }
        public static WorkTaskVar GetInstance()
        {
            return new WorkTaskVar();
        }
        #endregion
        #region  公有方法
        #region  基本方法
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Insert(WorkTaskVarEntity entity)
        {
            try
            {
                setInsertSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Update(WorkTaskVarEntity entity)
        {
            try
            {
                setUpdateSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string workTaskVarId)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            SqlDataItem sqlItem = new SqlDataItem();
            sqlItem.CommandText = tmpSql;
            sqlItem.AppendParameter("@" + keyField, workTaskVarId);
            ClientDBAgent agent = new ClientDBAgent();
            return agent.RecordExists(sqlItem);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="workTaskVarId">主键Id</param>
        /// <returns>影响的记录条数</returns>
        public void Delete(string workTaskVarId)
        {
            try
            {
                string tmpSql = "delete from " + tableName + " where  " + keyField + "=@" + keyField;
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, workTaskVarId);
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一条数据记录
        /// </summary>
        /// <param name="WorkTaskVarId">主键Id</param>
        /// <returns>DataTable</returns>
        public DataTable GetWorkTaskVarTable(string workTaskVarId)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, workTaskVarId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一个数据实体
        /// </summary>
        /// <param name="workTaskVarId">主键Id</param>
        /// <returns>DataTable</returns>
        public WorkTaskVarEntity GetWorkTaskVarEntity(string workTaskVarId)
        {
            DataTable dt = GetWorkTaskVarTable(workTaskVarId);
            if (dt != null && dt.Rows.Count > 0)
            {
                return GetWorkTaskVarEntity(dt.Rows[0]);
            }
            return null;
        }

        #endregion
        #region  扩展方法
        //此处添加扩展方法
        /// <summary>
        /// 获得流程模板的变量，不包含任务变量。
        /// </summary>
        /// <param name="workFlowId">流程模板Id</param>
        /// <returns>DataTable</returns>
        public  DataTable GetVarsByWorkFlow(string workFlowId)
        {
            try
            {
                string tmpSql = "select * from HF_WorkTaskVar where workflowId = @workflowId and AccessType=@AccessType";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@workflowId", workFlowId);
                sqlItem.AppendParameter("@AccessType", WorkFlowConst.Access_WorkFlow);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 删除流程的流程变量，不删除任务变量
        /// </summary>
        /// <param name="workFlowId">流程Id</param>
        public void DeleteVarByWorkFlow(string workFlowId)
        {
            try
            {
                string tmpSql = "delete from HF_WorkTaskVar where workflowId = @workflowId and AccessType=@AccessType";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@workflowId", workFlowId);
                sqlItem.AppendParameter("@AccessType", WorkFlowConst.Access_WorkFlow);
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获得流程模板的变量，不包含任务变量。
        /// </summary>
        /// <param name="workFlowId">流程模板Id</param>
        /// <returns>DataTable</returns>
        public List<WorkTaskVarEntity> GetVarEntitysByWorkFlow(string workFlowId)
        {

            List<WorkTaskVarEntity> result = new List<WorkTaskVarEntity>();
            DataTable dt = GetVarsByWorkFlow(workFlowId);
            try
            {
                foreach (DataRow dr in dt.Rows)
                {
                    WorkTaskVarEntity entity = GetWorkTaskVarEntity(dr);
                    result.Add(entity);
                }
                return result;

            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        /// <summary>
        /// 获得任务节点的所有任务变量
        /// </summary>
        /// <param name="workTaskId">任务模板Id</param>
        /// <returns></returns>
        public  DataTable GetVarsByWorkTask(string workTaskId)
        {
            try
            {
                string tmpSql = "select * from HF_WorkTaskVar where WorkTaskId = @WorkTaskId and AccessType=@AccessType";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@WorkTaskId", workTaskId);
                sqlItem.AppendParameter("@AccessType", WorkFlowConst.Access_WorkTask);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获得任务节点的所有任务变量
        /// </summary>
        /// <param name="workTaskId">任务模板Id</param>
        /// <returns>Entitys</returns>
        public List<WorkTaskVarEntity> GetVarEntitysByWorkTask(string workTaskId)
        {

            List<WorkTaskVarEntity> result = new List<WorkTaskVarEntity>();
            DataTable dt = GetVarsByWorkTask(workTaskId);
            try
            {
                foreach (DataRow dr in dt.Rows)
                {
                    WorkTaskVarEntity entity = GetWorkTaskVarEntity(dr);
                    result.Add(entity);
                }
                return result;

            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        /// <summary>
        /// 根据变量名称获得变量信息。变量名相同的任务变量排在最前。
        /// </summary>
        ///  <param name="workflowId">流程模板Id</param>
        /// <param name="varName">变量名</param>
        /// <returns>任务变量列表DataTable</returns>
        public  DataTable GetVarsByName(string workFlowId,string varName)
        {
            try
            {
                string tmpSql = "select * from HF_WorkTaskVar where VarName =@VarName and workFlowId=@workFlowId order by AccessType ";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@VarName", varName);
                sqlItem.AppendParameter("@workFlowId", workFlowId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 根据变量名称获得变量信息。变量名相同的任务变量排在最前。
        /// </summary>
        ///  <param name="workflowId">流程模板Id</param>
        /// <param name="varName">变量名</param>
        /// <returns>任务变量Entity</returns>
        public WorkTaskVarEntity GetVarEntitysByName(string workFlowId, string varName)
        {
            WorkTaskVarEntity result = null;
            DataTable dt = GetVarsByName(workFlowId,varName);
            try
            {
                if (dt!=null&&dt.Rows.Count>0)
                {
                    result = GetWorkTaskVarEntity(dt.Rows[0]);
                }
                return result;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获得流程模板中的所有变量，包括流程变量和任务变量
        /// </summary>
        /// <param name="workflowId">流程模板Id</param>
        /// <returns></returns>
        public  DataTable GetAllVarsByWorkFlow(string workFlowId)
        {
            try
            {
                string tmpSql = "select * from HF_WorkTaskVar where workflowId = @workflowId order by VarName";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@workflowId", workFlowId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获得流程模板中的所有变量，包括流程变量和任务变量
        /// </summary>
        /// <param name="workflowId">流程模板Id</param>
        /// <returns>Entitys</returns>
        public List<WorkTaskVarEntity> GetAllVarEntitysByWorkFlow(string workFlowId)
        {
            List<WorkTaskVarEntity> result = new List<WorkTaskVarEntity>();
            DataTable dt = GetAllVarsByWorkFlow(workFlowId);
            try
            {
                foreach (DataRow dr in dt.Rows)
                {
                    WorkTaskVarEntity entity = GetWorkTaskVarEntity(dr);
                    result.Add(entity);
                }
                return result;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        #endregion
    }
}

